package com.ncd.disease.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ncd.common.core.text.Convert;
import com.ncd.disease.domain.RetAdm;
import com.ncd.disease.domain.RetPatient;
import com.ncd.disease.mapper.RetAdmMapper;
import com.ncd.disease.param.RetAdmParam;
import com.ncd.disease.result.RetAdmResult;
import com.ncd.disease.service.IRetAdmService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 复诊Service业务层处理
 * 
 * @author zhouxin
 * @date 2024-03-10
 */
@Service
public class RetAdmServiceImpl extends ServiceImpl<RetAdmMapper,RetAdm> implements IRetAdmService
{
    @Resource
    private RetAdmMapper retAdmMapper;


    /**
     * 查询复诊列表
     * 
     * @param retAdm 复诊
     * @return 复诊
     */
    @Override
    public List<RetAdmResult> selectRetAdmList(RetAdm retAdm)
    {
        QueryWrapper<RetAdm> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(retAdm.getAdmStatus()),"t1.adm_status",retAdm.getAdmStatus());
        wrapper.eq(ObjectUtil.isNotNull(retAdm.getPatId()),"t1.pat_id",retAdm.getPatId());
        wrapper.eq(ObjectUtil.isNotNull(retAdm.getPatDiaId()),"t1.pat_dia_id",retAdm.getPatDiaId());
        wrapper.eq(ObjectUtil.isNotEmpty(retAdm.getAdmType()),"t1.adm_type",retAdm.getAdmType());
        wrapper.orderByAsc("t1.adm_date");
        return retAdmMapper.selectRetAdmList(wrapper);
    }

    /**
     * 新增复诊
     * 
     * @param retAdm 复诊
     * @return 结果
     */
    @Override
    public int insertRetAdm(RetAdm retAdm)
    {
        retAdm.setAdmCreateDate(new Date());
        return retAdmMapper.insert(retAdm);
    }

    /**
     * 修改复诊
     *
     * @return 结果
     */
    @Override
    public int updateRetAdm(RetAdmParam param)
    {
        if("Y".equals(param.getAllFlag())){
            List<RetAdm> retAdms=this.list(new LambdaQueryWrapper<>(RetAdm.class).ne(RetAdm::getAdmStatus,param.getStatus()));
            this.update(new LambdaUpdateWrapper<>(RetAdm.class)
                    .set(RetAdm::getAdmStatus,param.getStatus())
                    .in(RetAdm::getAdmId, retAdms.stream().map(adm->adm.getAdmId()).collect(Collectors.toList())));
        }else{
            this.update(new LambdaUpdateWrapper<>(RetAdm.class)
                    .set(RetAdm::getAdmStatus,param.getStatus())
                    .in(RetAdm::getAdmId, Convert.toLongList(param.getIds())));
        }
        return 1;
    }

}
